<template>
  <el-form-item label="自动排序设置" v-if="optionModel.isAutoSort">
    <el-input v-model="optionModel.subAutoSort" placeholder="请输入自动排序设置" @click="showSortDialog" readonly />
    <el-dialog v-model="sortDialogShowFlag" title="排序" width="400" :before-close="handleClose">
      <el-form ref="form" :model="form" label-width="auto" style="max-width: 600px" :rules="rules">
        <el-form-item label="排序字段" prop="sortCode">
          <el-select v-model="form.sortCode" placeholder="请选择排序字段">
            <el-option v-for="item in subFormJson" :label="item.name" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="排序顺序" prop="sortBy">
          <el-radio-group v-model="form.sortBy">
            <el-radio value="1" size="large">正序</el-radio>
            <el-radio value="2" size="large">倒序</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="sortDialogShowFlag = false">取消</el-button>
          <el-button type="primary" @click="changeSort">
            确定
          </el-button>
        </div>
      </template>
    </el-dialog>
  </el-form-item>
</template>

<script>
import i18n from "@/utils/i18n"

export default {
  name: "sub-form-sort-editor",
  mixins: [i18n],
  data() {
    return {
      sortDialogShowFlag: false,
      form: {
        sortCode: "",
        sortBy: ""
      },
      rules: {
        sortCode: [
          { required: true, message: 'Please input Activity name', trigger: 'blur' },
        ],
        sortBy: [
          { required: true, message: 'Please input Activity name', trigger: 'blur' },
        ],
      },
      subFormJson:[]
    }
  },
  props: {
    designer: Object,
    selectedWidget: Object,
    optionModel: Object,
  },
  methods: {
    showSortDialog() {
      this.sortDialogShowFlag = true;
      this.selectedWidget.widgetList.forEach(item => {
        this.subFormJson.push({
          name:item.options.label,
          value:item.options.keyName || item.options.name
        })
      })
    },
    changeSort() {
      this.$refs.form.validate((valid, fields) => {
        if (valid) {
          this.optionModel.subAutoSort = this.subFormJson.find(item => item.value === this.form.sortCode).name + "(" + ((this.form.sortBy == 1 ? '正序' : '倒序') + ")")
          this.optionModel.subAutoSortCode = this.form;
          this.sortDialogShowFlag = false;
        } else {
          console.log('error submit!', fields)
        }
      })
    }
  }
}
</script>

<style scoped></style>
